// pages/staff_home/index/detail.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_id: 0,
        genre: 0,
        genre_name: '',
        action_date: '',
        action_time: '',
        frequency_name: '',
        liaison_name: '',
        liaison_phone: '',
        position_lat: 0,
        position_lng: 0,
        position_markers: [],
        address: '',
        address_describe: '',
        create_time: '',
        remark: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 获取传入参数
        this.data.order_id = parseInt(options.id);

        // 验证登录
        app.verifyLogin();

        // 获取参数
        let page = this;
        app.execute(function() {
            let page = app.getPage();
            page.getInfo(page);
        });
    },

    /**
     * 自定义函数--获取信息
     */
    getInfo: function(page) {

        wx.showLoading({
            title: '获取数据',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_order/get_detail',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: {
                id: page.data.order_id
            },
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        let page = app.getPage();
                        response.data.data.position_markers = new Array();
                        response.data.data.position_markers.push({
                            latitude: response.data.data.position_lat,
                            longitude: response.data.data.position_lng,
                            width: 50,
                            height: 50
                        });
                        page.setData(response.data.data);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--拨打电话
     */
    callPhone: function(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        });
    },

    /**
     * 自定义函数--接取任务
     */
    accept: function() {
        let page = this;
        wx.showLoading({
            title: '处理中...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_order/action_accept',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: {
                id: page.data.order_id
            },
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                        if (0 === response.data.errno) {
                            setTimeout(function() {
                                wx.reLaunch({
                                    url: '/pages/staff_home/index/index',
                                });
                            }, 1000);
                        };
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    }

});